package vip.xiaonuo.smzq.modular.jsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.locationtech.jts.geom.Point;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.core.utils.TransFormUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb06Kc;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb06KcMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.hisi.entity.vo.Kcdgmtj;
import vip.xiaonuo.smzq.modular.hisi.mapper.HisiKsMapper;
import vip.xiaonuo.smzq.modular.hisi.service.IHisiKsService;
import vip.xiaonuo.smzq.modular.jsc.entity.*;
import vip.xiaonuo.smzq.modular.jsc.entity.vo.*;
import vip.xiaonuo.smzq.modular.jsc.mapper.KczlMapper;
import vip.xiaonuo.smzq.modular.jsc.mapper.KczylyMapper;
import vip.xiaonuo.smzq.modular.jsc.mapper.KyqMapper;
import vip.xiaonuo.smzq.modular.jsc.mapper.XxlbMapper;
import vip.xiaonuo.smzq.modular.jsc.service.IKczlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.jsc.service.IZyyskcService;
import vip.xiaonuo.smzq.modular.kcl.entity.Ks;
import vip.xiaonuo.smzq.modular.kcl.mapper.KsMapper;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 驾驶舱矿产种类统计 服务实现类
 *
 * @author sqq
 * @since 2024-05-13
 */
@Service
public class KczlServiceImpl extends ServiceImpl<KczlMapper, Kczl> implements IKczlService {

    @Resource
    private KczlMapper kczlMapper;
    @Resource
    private UserMapper userMapper;//用户
    @Resource
    private KyqMapper kyqMapper;//矿业权
    @Resource
    private HisiKsMapper hisiKsMapper;
    @Resource
    private IZyyskcService iZyyskcService; //主要优势矿产
    @Resource
    private IHisiKsService iHisiKsService; //矿山信息统计
    @Resource
    private XxlbMapper xxlbMapper;//热点矿产
    @Resource
    private KczylyMapper kczylyMapper;//矿产资源利用
    @Resource
    private Cb06KcMapper cb06KcMapper; //矿产
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;//行政区划
    @Resource
    private KsMapper ksMapper; //矿山信息

    @Override
    public Page<Kczl> page(Integer nd, String xzqhdm) {
        QueryWrapper<Kczl> queryWrapper = new QueryWrapper<>();
        if (nd != null) {
            queryWrapper.eq("nd", nd);
        }
        if (xzqhdm != null) {
            queryWrapper.eq("xzqhdm", xzqhdm);
        }

        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public LdjscData getLdjscData(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());

        //行政区代码
        String xzqh = String.valueOf(user.getOrgId());
        if (user.getOrgId().equals("0")||user.getOrgId().equals("-1")){
            xzqh="130000";
        }

        xzqh = xzqh.replaceAll("0+$", "");

        LdjscData returnData = new LdjscData();
        //基本情况
        Xxlb xxlb = xxlbMapper.selectOne(new QueryWrapper<Xxlb>().eq("nd", nd).eq("xzqhdm", user.getDirectorId()).eq("xxlx", 306001));
        if (xxlb != null) {
            returnData.setJbqk(xxlb.getXxnr());
            returnData.setJbqkxq(xxlb.getXxnrxq());
        }


        //热点矿产
        List<Xxlb> xxlbList = xxlbMapper.selectList(new QueryWrapper<Xxlb>().eq("nd", nd).eq("xzqhdm", user.getDirectorId()).eq("xxlx", 306002));
        if (xxlbList != null && xxlbList.size() > 0) {
            returnData.setRdkcList(xxlbList);
        }

        // 获取矿产种类统计
        Kczl kczl = kczlMapper.selectOne(new QueryWrapper<Kczl>().eq("nd", nd).eq("xzqhdm", user.getDirectorId()).eq("kclx", 0));
        if (kczl != null) {
            returnData.setKczl(kczl);
        }
        // 获取战略矿产种类统计
        Kczl zlkczl = kczlMapper.selectOne(new QueryWrapper<Kczl>().eq("nd", nd).eq("xzqhdm", user.getDirectorId()).eq("kclx", 1));
        if (kczl != null) {
            returnData.setZlkczl(zlkczl);
        }
        //矿业权
        List<Kyq> kyqList = kyqMapper.selectList(new QueryWrapper<Kyq>().eq("nd", nd).like("xzqhdm", xzqh).orderByAsc("xzqhdm"));
        if (kyqList != null && kyqList.size() > 0) {
            returnData.setKyqList(kyqList);
        }

        // 矿地规模统计
        String sql = "";
        if (nd != null && nd > 0) {
            sql += " and nd=" + nd;
        }
        sql += " and xzqdm like'%" + xzqh + "%'";
        Kcdgmtj kcdgmtj = hisiKsMapper.selectKcdgmtj(sql);

        if (kcdgmtj != null) {
            kcdgmtj.setXzqhdm(user.getDirectorId());
            returnData.setKcdgmtj(kcdgmtj);
        }

        //主要优势矿产
        List<Zyyskc> zyyskcList = iZyyskcService.getZyyskcList(nd, user.getDirectorId());
        if (zyyskcList != null && zyyskcList.size() > 0) {
            returnData.setZyyskcList(zyyskcList);
        }

        //矿产分布
        //TODO 接口拆分、此接口单独优化
        List<Kcfb> kcfbList = iHisiKsService.getkcfbList(nd, user.getDirectorId());
        if (kcfbList != null && kcfbList.size() > 0) {
            returnData.setKcfbList(kcfbList);
        }

//        //矿产资源利用
//        Kczyly kczyly=    kczylyMapper.selectOne(new QueryWrapper<Kczyly>().eq("nd", nd).eq("xzqhdm", user.getDirectorId()));
//        if (kczyly != null){
//            returnData.setKczyly(kczyly);
//        }
        //矿产利用率
        Kclyl kclyl = getKclyl(nd, xzqh);
        if (kclyl != null){
            returnData.setKclyl(kclyl);
        }

        return returnData;
    }

    private Kclyl getKclyl(Integer nd, String xzqhdm) {

        try {
            Kclyl kclyl = new Kclyl();
            String[] kcdms = {"22001", "11001", "42201", "32017", "63941", "83906"};
            DecimalFormat df = new DecimalFormat("#.00");  // 创建一个格式化对象，保留两位小数

            //铁矿
            double tklyl = hisiKsMapper.getlyl(nd, xzqhdm, "22001");
            kclyl.setTklyl(df.format(tklyl * 100));
            //煤矿利用率
            double meiklyl = hisiKsMapper.getlyl(nd, xzqhdm, "11001");
            kclyl.setMeiklyl(df.format(meiklyl * 100));
            //金矿利用率
            double jklyl = hisiKsMapper.getlyl(nd, xzqhdm, "42201");
            kclyl.setJklyl(df.format(jklyl * 100));
            //钼矿利用率
            double muklyl = hisiKsMapper.getlyl(nd, xzqhdm, "32017");
            kclyl.setMuklyl(df.format(muklyl * 100));
            //冶金用白云岩矿利用率
            double yjybhylyl = hisiKsMapper.getlyl(nd, xzqhdm, "63941");
            kclyl.setYjybhylyl(df.format(yjybhylyl * 100));
            //水泥用灰岩矿利用率
            double snyhyklyl = hisiKsMapper.getlyl(nd, xzqhdm, "22001");
            kclyl.setSnyhyklyl(df.format(snyhyklyl * 100));
            return kclyl;
        } catch (Exception e) {
            e.printStackTrace();
        }


       return null;
    }

    @Override
    public void add(Kczl kczl) {
        if (kczl.getXzqhdm() != null && kczl.getNd() != null && kczl.getKclx() != null) {
            kczlMapper.delete(new QueryWrapper<Kczl>().eq("nd", kczl.getNd()).eq("xzqhdm", kczl.getXzqhdm()).eq("kclx", kczl.getKclx()));
            kczlMapper.insert(kczl);
        }

    }

    @Override
    public List<Kczl> KczlList(Integer nd, String xzqhdm, Integer kclx) {

        QueryWrapper<Kczl> queryWrapper = new QueryWrapper<>();
        if (nd != null) {
            queryWrapper.eq("nd", nd);
        }
        if (xzqhdm != null) {
            queryWrapper.eq("xzqhdm", xzqhdm);
        }
        if (kclx != null) {
            queryWrapper.eq("kclx", kclx);
        }


        return kczlMapper.selectList(queryWrapper);
    }

    /**
     * 驾驶舱首页-基本情况
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public LdjscDataJbqk getLdjscDataJbqk(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        LdjscDataJbqk returnData = new LdjscDataJbqk();
        //基本情况
        Xxlb xxlb = xxlbMapper.selectOne(new QueryWrapper<Xxlb>().eq("nd", nd).eq("xzqhdm", user.getOrgId()).eq("xxlx", 306001));
        if (xxlb != null) {
            returnData.setJbqk(xxlb.getXxnr());
            returnData.setJbqkxq(xxlb.getXxnrxq());
        }
        return returnData;
    }

    /**
     * 驾驶舱首页-热点矿产
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public List<Xxlb> getLdjscDataRdkc(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        return xxlbMapper.selectList(new QueryWrapper<Xxlb>().eq("nd", nd).eq("xzqhdm", user.getOrgId()).eq("xxlx", 306002));
    }

    /**
     * 驾驶舱首页-矿产种类统计、战略矿产种类统计
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public LdjscDataKczl getLdjscDataKczl(Integer nd) {

        LdjscDataKczl returnData = new LdjscDataKczl();
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        // 获取矿产种类统计
        Kczl kczl = kczlMapper.selectOne(new QueryWrapper<Kczl>().eq("nd", nd).eq("xzqhdm", user.getOrgId()).eq("kclx", 0));
        if (kczl != null) {
            returnData.setKczl(kczl);
        }
        // 获取战略矿产种类统计
        Kczl zlkczl = kczlMapper.selectOne(new QueryWrapper<Kczl>().eq("nd", nd).eq("xzqhdm", user.getOrgId()).eq("kclx", 1));
        if (kczl != null) {
            returnData.setZlkczl(zlkczl);
        }
        return returnData;
    }

    /**
     * 获取行政区划代码
     *
     * @param orgId
     * @author Kevin
     * @since 2024/7/19 10:13
     */
    public String getXzqh(String orgId) {
        String xzqh = String.valueOf(orgId);
        if ("0".equals(orgId) || "-1".equals(orgId)){
            xzqh="130000";
        }
        xzqh = xzqh.replaceAll("0+$", "");
        return xzqh;
    }

    /**
     * 驾驶舱首页-矿业权
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public List<Kyq> getLdjscDataKyq(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        //行政区代码
        String xzqh = getXzqh(user.getOrgId());

        //矿业权
        return kyqMapper.selectList(new QueryWrapper<Kyq>().eq("nd", nd).like("xzqhdm", xzqh).orderByAsc("xzqhdm"));
    }

    /**
     * 驾驶舱首页-矿产地规模
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public Kcdgmtj getLdjscDataKcdgm(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        //行政区代码
        String xzqh = getXzqh(user.getOrgId());

        // 矿地规模统计
        String sql = "";
        if (nd != null && nd > 0) {
            sql += " and ks.nd=" + nd;
        }
        sql += " and ks.xzqdm like'%" + xzqh + "%'";
        Kcdgmtj kcdgmtj = hisiKsMapper.selectKcdgmtjNew(sql);

        if (kcdgmtj != null) {
            kcdgmtj.setXzqhdm(user.getOrgId());
        }

        return kcdgmtj;
    }

    /**
     * 驾驶舱首页-主要优势矿产
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public List<Zyyskc> getLdjscDataZyyskc(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        //主要优势矿产
        return iZyyskcService.getZyyskcList(nd, user.getOrgId());
    }

    /**
     * 驾驶舱首页-矿产分布
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public List<Kcfb> getLdjscDataKcfb(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        //矿产分布
        //TODO 此接口需要优化
        return iHisiKsService.getkcfbList(nd, user.getOrgId());
    }

    /**
     * 驾驶舱首页-矿产利用率
     *
     * @param nd
     * @author Kevin
     * @since 2024/7/19 9:46
     */
    @Override
    public Kclyl getLdjscDataKclyl(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();

        //行政区代码
        String xzqh = getXzqh(user.getOrgId());

//        //矿产资源利用
//        Kczyly kczyly=    kczylyMapper.selectOne(new QueryWrapper<Kczyly>().eq("nd", nd).eq("xzqhdm", user.getOrgId()));
//        if (kczyly != null){
//            returnData.setKczyly(kczyly);
//        }
        //矿产利用率
        return getKclyl(nd, xzqh);
    }

    @Override
    public List<Kcfb> getLdjscDataKzfb(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();
        String[] kcdms = {"22001", "11001", "42201", "32017", "63941", "83906"};
        List<String> kcdmList = Arrays.asList(kcdms);
        List<Kcfb> kcfbList = new ArrayList<>();
        for (String kcdm : kcdmList){
            Kcfb kcfb = new Kcfb();
            List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(new QueryWrapper<Cb06Kc>().eq("kcdm", Integer.valueOf(kcdm)));
            Cb06Kc cb06Kc = cb06KcList.get(0);
            kcfb.setKcdm(kcdm);
            kcfb.setKcmc(cb06Kc.getKcmc());
//            List<Cb08Xzqh> cb08XzqhList = cb08XzqhMapper.selectList(new QueryWrapper<Cb08Xzqh>().eq("fdm", Integer.valueOf(user.getOrgId())));
//            for (Cb08Xzqh cb08Xzqh : cb08XzqhList) {
//                List<Cb08Xzqh> cb08XzqhxianList = cb08XzqhMapper.selectList(new QueryWrapper<Cb08Xzqh>().eq("fdm", cb08Xzqh.getDm()));
//                for (Cb08Xzqh cb08Xzqhxian : cb08XzqhxianList) {
//                    if (!cb08Xzqhxian.getMc().equals("市辖区")) {
                        LambdaQueryWrapper<Ks> ksQueryWrapper = new LambdaQueryWrapper<>();
                        ksQueryWrapper.eq(Ks::getNd, nd);
//                        ksQueryWrapper.eq(Ks::getXzqdm, cb08Xzqhxian.getDm());
//                        ksQueryWrapper.eq(Ks::getZkcm, cb06Kc.getKcdm());
                        List<Ks> ksList = ksMapper.selectList(ksQueryWrapper);
                        for (Ks ks : ksList){
                            if (ks.getZxdxzb() != null && ks.getZxdyzb() != null) {
                                DecimalFormat df = new DecimalFormat("#.##########");
                                Point point =new TransFormUtil().ConvertCoordinates4490(df.format(ks.getZxdxzb()), df.format(ks.getZxdyzb()));
                                if (point != null) {
                                    ks.setZxdxzb(point.getX());
                                    ks.setZxdyzb(point.getY());
                                }
                            }
                        }

                        kcfb.setKzdwfbList(ksList);
//                    }
//                }
//            }
            kcfbList.add(kcfb);
        }
        return kcfbList;
    }
}
